package com.lanswon.qzsmk.controller.cardManageController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.base.HttpContants;
import com.lanswon.qzsmk.exception.ApplicationException;
import com.lanswon.qzsmk.model.FeeRules;
import com.lanswon.qzsmk.model.Personal;
import com.lanswon.qzsmk.service.appManageService.AppFeeService;
import com.lanswon.qzsmk.service.cardManageService.ChangeCardService;
import com.lanswon.qzsmk.service.cardManageService.SupplementCardService;
import com.lanswon.qzsmk.vo.FeeRulesVo;
import com.lanswon.qzsmk.vo.ResultVo;
import com.lanswon.qzsmk.vo.SupplementCardInfo;
import com.lanswon.qzsmk.vo.VoucherVo;

/**
 * 补卡controller类
 * @author guyw
 *
 */
@RequestMapping(value="/reissueCard")
@Controller
public class ReissueCardController {
	
	@Autowired
	SupplementCardService sc;
	
    @Autowired
	ChangeCardService ccs;
    
    @Autowired
    AppFeeService afs;
	
	/**
	 * 链接补卡页面
	 * @return
	 */
    @RequiresPermissions("reissueCard:index")
	@RequestMapping(value="/goReissueCard",name="补卡,reissueCard:index",method={RequestMethod.GET,RequestMethod.POST})
	public String goReissueCard(){
		return "ReissueCard";
	}
	
	/**
	 * 获取要补卡的信息
	 * @return list
	 */
    @SuppressWarnings("unchecked")
	@RequiresPermissions("reissueCard:getReissueCardInfo")
	@RequestMapping(value="/getReissueCardInfo",name="补卡查询补卡信息,reissueCard:getReissueCardInfo",method=RequestMethod.POST)
	@ResponseBody
	public DwzPage<SupplementCardInfo> getReissueCardInfo(SupplementCardInfo sci,Integer numPerPage,Integer pageNum){
//		System.out.println(sci.getPaperNo()+";"+sci.getCurrentPage()+";"+sci.getNumPerPage()+";"+pageCount);
		DwzPage<SupplementCardInfo> dPage=null;
//		if(sci.getNumPerPage()==null){//加载页面时执行
//			sci.setCurrentPage(1);
//			sci.setNumPerPage(20);
//		}else {
//			if(sci.getCurrentPage()<=1||pageCount==0){
//				sci.setCurrentPage(1);
//			}else if(sci.getCurrentPage()>=pageCount&&pageCount!=0){
//				sci.setCurrentPage(pageCount);
//			}
//		}
		if(pageNum==null&&numPerPage==null){
			pageNum=1;
			numPerPage=20;	
		}
		sci.setCurrentPage(pageNum);
		sci.setNumPerPage(numPerPage);
		sci.setStartLine((sci.getCurrentPage()-1)*sci.getNumPerPage());
		ResultVo resultVo=sc.querySupplementCard(sci);
		if(resultVo.getResultStatus().getCode()==HttpContants.RETURN_SUCCESS){
			dPage=(DwzPage<SupplementCardInfo>) resultVo.getServiceObject();
		}
		return dPage;
	}
	
	
	/**
	 * 链接补卡登记页面
	 * @return
	 */
    @RequiresPermissions("reissueCard:reissueRegister")
	@RequestMapping(value="/reissueRegister",name="补卡登记页面,reissueCard:reissueRegister",method=RequestMethod.POST)
	public String reissueRegister(){
		return "ReissueRegister";
	}
	
	/**
	 * 获取补卡登记页面需要的详细信息并跳转补卡登记页面
	 * @param parm
	 * @return
	 * @throws ApplicationException 
	 */
    @RequiresPermissions("reissueCard:goReissueRegister")
	@RequestMapping(value="/goReissueRegister",name="补卡获取补卡登记页面需要的详细信息,reissueCard:goReissueRegister",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> goReissueRegister(String[] reissueCard,HttpServletRequest request) throws Exception{
		Map<String, Object> map=new HashMap<>();
		System.out.print(reissueCard[0]+";;;"+reissueCard[1]+";;;"+reissueCard[2]+";;;"+reissueCard[3]);
		map.put("cardpName", reissueCard[1]);
		map.put("cardNo", reissueCard[0]);
		Personal p=sc.queryPersonalByPaperNo(reissueCard[2], reissueCard[3],reissueCard[1]);
		if(p!=null){
			p.setPaperType(reissueCard[2]);
			map.put("p", p);
		}
		request.getSession().setAttribute("registerInfo", map);
		map.put("statusCode", "200");
		map.put("callbackType", "forward");
		map.put("forwardUrl", "reissueCard/reissueRegister");//post请求	
		return map;
	}
	
	/**
	 * 补卡登记页面获取详细信息
	 * @param request
	 * @return
	 */
    @RequiresPermissions("reissueCard:getRegisterInfo")
	@RequestMapping(value="/getRegisterInfo",name="补卡登记页面,reissueCard:getRegisterInfo",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> getRegisterInfo(HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> map=(Map<String, Object>) request.getSession().getAttribute("registerInfo");
		request.getSession().removeAttribute("registerInfo");
		return map;
	}
	
	/**
	 * 补卡登记提交
	 * @param sci
	 * @param SupplementCardTime
	 * @param getCardTime
	 * @return
	 * @throws ParseException 
	 */
    @RequiresPermissions("reissueCard:commitRegisterInfo")
	@RequestMapping(value="/commitRegisterInfo",name="补卡提交,reissueCard:commitRegisterInfo",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> commitRegisterInfo(SupplementCardInfo sci,HttpServletRequest request){
		System.out.println(sci);
		VoucherVo vvo=null;
		ResultVo resultVo=sc.commitSupplementCardInfo(sci);//需要改动,跳转到凭证页面
		Map<String, Object>map=new HashMap<>();
		if(resultVo.getResultStatus().getCode()==HttpContants.RETURN_SUCCESS){
			vvo=(VoucherVo) resultVo.getServiceObject();
			//map.put("vvo", vvo);
			//map.put("statusCode", "200");
			//map.put("message", "操作成功");
			//map.put("callbackType", "forward");
			//map.put("forwardUrl", "reissueCard/goReissueCard");//post请求
			request.getSession().setAttribute("vo", vvo);
			request.getSession().setAttribute("type", "supplierCardRegister");
	      	map.put("max", "true");
	        map.put("title", "补卡凭证");
	        map.put("rel", "reissue");
	        map.put("statusCode", "200");
	        map.put("url", "printCredentials/toHairpinPdfView");
	        //20181204 hgl 增加以下3行
	        map.put("jumpToTile", "补卡");
	        map.put("jumpToTabid", "reissueCard");
	        map.put("jumpToUrl", "reissueCard/goReissueCard");
		}else{
			map.put("statusCode", "300");
			map.put("message", resultVo.getResultStatus().getMessage());
		}
		
		return map;
		
	}
	
}
